\bigskip
\bigskip
{\magonebf 6.1.2 Segments (segment)}

An instance $s$ of the data type $segment$ is a directed straight line 
segment in the two-dimensional plane, i.e., a straight line segment $[p,q]$ 
connecting two points $p,q \in \real^2$. $p$ is called the start point
and $q$ is called the end point of $s$. The length of $s$ is the euclidean 
distance between $p$ and $q$. The angle between a right oriented horizontal 
ray and $s$ is called the direction of $s$. The segment $[(0,0),(0,0)]$ 
is said to be empty.

\def\name{$segment$}
\def\type{$segment$}

{\bf 1. Creation of a segment }

a) \create s (point\ p,\ point\ q)

b) \create s (point\ p,\ real\ \alpha,\ real\ d)

c) \create s {}


introduces a variable \var\ of type \name. \var\ is initialized to the segment 
from $p$ to $q$ (variant a), to the segment with start point $p$, direction 
$\alpha$, and length $d$ (variant b) or to the empty segment (variant c).

\vfill\eject

\bigskip
{\bf 2. Operations on a segment \var}

\medskip
\+\cleartabs & \hskip 2.5truecm & \hskip 5truecm &\cr
\+\op point   start {}
                           {returns the start point of segment \var.}
\smallskip
\+\op point   end {}
                           {returns the end point of segment \var.}
\smallskip
\+\op real    xcoord1 {}
                           {returns the x-coordinate of \var.start().}
\smallskip
\+\op real    ycoord1 {}
                           {returns the y-coordinate of \var.start().}
\smallskip
\+\op real    xcoord2 {}
                           {returns the x-coordinate of \var.end().}
\smallskip
\+\op real    ycoord2 {}
                           {returns the y-coordinate of \var.end().}
\smallskip
\+\op real    length {}
                           {returns the length of \var.}
\smallskip
\+\op real   direction {} 
                        {returns the direction of \var\ as an angle in}
\+\nop                  {the intervall $(-\pi,\pi]$.}
\smallskip
\+\op real   angle {segment\ t} 
                        {returns the angle between \var\ and $t$, i.e.,}
\+\nop                  {$t$.direction() - \var.direction().}
\smallskip
\+\op real   angle {} 
                        {returns \var.direction().}
\smallskip
\+\op bool   horizontal {} 
                        {returns true iff \var\ is horizontal.}
\smallskip
\+\op bool   vertical {} 
                        {returns true iff \var\ is vertical.}
\smallskip
\+\op real   slope {}
                        {returns the slope of $s$.}
\+\nop                  { \precond  \var\  is not vertical.}
\smallskip
\+\op bool    intersection {segment\ t,\ point\&\ p} {}
\+\nop                     {if $s$ and $t$ are not collinear and intersect the}
\+\nop                     {intersection point is assigned to $p$ and true is}
\+\nop                     {returned, otherwise false is returned.}
\smallskip
\+\op segment rotate {point\ q,\ real\ \alpha}
                        {returns the segment created by a rotation of $s$}
\+\nop                  {about point $q$ by angle $\alpha$.}
\smallskip
\+\op segment rotate {real\ \alpha}
                        {returns $s$.rotate($s$.start(),$\alpha$).}
\smallskip
\+\op segment translate {vector\ v} {}
\+\nop                     {returns $s+v$, i.e., the segment created by}
\+\nop                     {translating $s$ by vector $v$. \precond $v$}
\+\nop                     {has dimension 2.}
\smallskip
\+\op segment translate {real\ alpha,\ real\ d} {}
\+\nop                  {returns the segment created by a translation of }
\+\nop                  {$s$ in direction $\alpha$ by distance $d$.}
\smallskip

\bigskip
{\bf 3. Operators }
\medskip
\+&$segment\&$  &$segment$ =\ \  $segment$  &assignment\cr
\smallskip
\+&$bool$       &$segment$ ==    $segment$  &test for equality\cr
\smallskip
\+&$bool$       &$segment$\ !=   $segment$  &test for inequality\cr
\smallskip
\+&$segment$    &$segment$ +\ \  $vector$  &translation by vector\cr

\medskip
\smallskip

\medskip
Input and output operators:
\medskip
\+&$ostream\&$ &$ostream << segment$ &writes a segment to an output stream.\cr
\smallskip
\+&$istream\&$ &$istream >> segment$ &reads the coordinates of a segment (four reals)\cr
\+&            &                     &from an input stream.\cr

